package v1

import (
	"saas-app/app/define"
	"saas-app/app/model"
	"saas-app/app/service"
	"saas-app/library/response"

	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/util/gconv"
)

func GetStudentList(r *ghttp.Request) {

	page := r.GetQueryInt("page")
	limit := r.GetQueryInt("limit")
	school_id := r.GetQueryInt("school_id")
	campus_id := r.GetQueryInt("campus_id")

	result, total := service.StudentService().GetStudentList(school_id, campus_id, page, limit)
	response.JsonExit(r, 0, "ok", response.Paging(result, total, page, limit))
}

func UpdateStudent(r *ghttp.Request) {

	student_id := r.GetQueryUint("student_id")

	var data = make(map[string]interface{})

	if r.GetForm("student_name") != nil {
		data["student_name"] = r.GetFormString("student_name")
	}

	if r.GetForm("sex") != nil {
		data["sex"] = r.GetFormUint("sex")
	}

	if r.GetForm("avatar") != nil {
		data["avatar"] = r.GetFormString("avatar")
	}

	if r.GetForm("address") != nil {
		data["address"] = r.GetFormString("address")
	}

	if r.GetForm("school_name") != nil {
		data["school_name"] = r.GetFormString("school_name")
	}

	if r.GetForm("birthday") != nil {
		data["birthday"] = r.GetFormString("birthday")
	}

	if r.GetForm("remark") != nil {
		data["remark"] = r.GetFormString("remark")
	}

	if r.GetForm("status") != nil {
		data["status"] = r.GetFormString("status")
	}

	result := service.StudentService().UpdateStudent(student_id, data)
	response.JsonExit(r, 0, "ok", result)
}

func CreateStudent(r *ghttp.Request) {

	var (
		data         *define.StudentRequest
		studentParam *model.Student
	)

	if err := r.Parse(&data); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	if err := gconv.Struct(data, &studentParam); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	result := service.StudentService().CreateStudent(studentParam)
	response.JsonExit(r, 0, "ok", result)
}

func StudentCourselist(r *ghttp.Request) {

	campus_id := r.GetVar("campus_id").Int()
	student_id := r.GetQueryInt("student_id")
	page := r.GetQueryInt("page", 1)
	limit := r.GetQueryInt("limit", 10)

	result, total := service.StudentService().StudentCourselist(campus_id, student_id, page, limit)
	response.JsonExit(r, 0, "ok", response.Paging(result, total, page, limit))
}

func Attendance(r *ghttp.Request) {

	var (
		data *define.AttendanceParams
	)

	if err := r.Parse(&data); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	service.StudentService().Attendance(data)

	response.JsonExit(r, 0, "ok", nil)
}
